package com.javacasino.casino.util;

import com.javacasino.casino.service.impl.WebSocketServiceimpl;
import lombok.Data;
import org.java_websocket.enums.ReadyState;

import java.util.concurrent.Callable;

@Data
public class ThreadCallable implements Callable {

    /**
     * session
     */
    private String sid;
    /**
     *  控制下多少单默认 1 单
     */
    private int placeOrderNum = 1;
    /**
     * 重新登录字符json
     */

    private String longInfo;
    /**
     * 兑换点数字符json
     */
    private String convertJson;
    /**
     * 下单json
     */
    private String lineIfo;
    /**
     * 长链接对象
     */
    private WebSocketServiceimpl.CustomizedWebSocketClient client;

    /**
     *
     * @param mainNumber 主账号 sid
     * @param longInfo  登录字符
     * @param convertJson 兑换点数
     * @param placeOrderNum 一次下多少单
     * @param lineIfo  下单json
     * @param customizedWebSocketClient  长连接
     */
    public ThreadCallable(String mainNumber,String longInfo, String convertJson,int placeOrderNum, String lineIfo,  WebSocketServiceimpl.CustomizedWebSocketClient customizedWebSocketClient) {
        this.sid = mainNumber;
        this.placeOrderNum = placeOrderNum;
        this.client = customizedWebSocketClient;
        this.longInfo = longInfo;
        this.convertJson = convertJson;
        this.lineIfo = lineIfo;
    }


    @Override
    public Object call() throws Exception {
        long start = System.currentTimeMillis();
        boolean flag = false;
        if (client.isClosed()) {
            client.connect();
            client.send(longInfo);
            flag = true;
        }
        while (!ReadyState.OPEN.equals(client.getReadyState())) {
        }
        //兑换点数
        if (flag) {
            client.send(convertJson);
        }
        //开始下单
        for (int i = 0; i < placeOrderNum; i++) {
//            client.send(lineIfo);
        }
        System.out.println("下单次数----"+placeOrderNum+"----------耗时--"+(System.currentTimeMillis() - start));
        return sid;
    }



}
